package com.lesso.lpq.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lpq.sunflower.domain.repository.EmployeeRepository;
import com.lesso.lpq.sunflower.infra.assembler.EmployeeAssembler;
import com.lesso.lpq.sunflower.infra.mapper.EmployeeMapper;
import com.lesso.lpq.sunflower.model.Employee;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeDTO;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeQueryDTO;
import com.lesso.lpq.sunflower.pojo.vo.EmployeeVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * EmployeeRepositoryImpl
 *
 * @author zqz
 * @since 2025-07-02
 */
@Repository
public class EmployeeRepositoryImpl implements EmployeeRepository {
    private final EmployeeMapper employeeMapper;

    public EmployeeRepositoryImpl(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public Long save(Employee employee) {
        employee.setDelStatus(0);
        employeeMapper.insert(employee);
        return employee.getId();
    }

    @Override
    public Page<EmployeeVO> getPageList(EmployeeQueryDTO dto) {
        Page<EmployeeVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<Employee> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.hasText(dto.getName()), "name", dto.getName());
        wrapper.between("entry_date", dto.getEntryDateStart(), dto.getEntryDateEnd());
        List<Employee> employees = employeeMapper.selectPage(page, wrapper);
        return page.setRecords(EmployeeAssembler.INSTANCE.entityListToVOList(employees));
    }

    @Override
    public Page<EmployeeVO> getCustomPageList(EmployeeQueryDTO dto) {
        Page<EmployeeVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<EmployeeVO> list = employeeMapper.selectPageList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public void removeById(Long id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public void updateById(EmployeeDTO dto) {
        Employee entity = EmployeeAssembler.INSTANCE.dtoToEntity(dto);
        employeeMapper.updateById(entity);
    }

    @Override
    public EmployeeVO getByEmployeeNo(String employeeNo) {
//        EntityWrapper<Employee> wrapper = new EntityWrapper<>();
//        wrapper.eq("employee_no",employeeNo);
//        wrapper.eq("del_status",0);
//        Employee employee = employeeMapper.selectById(wrapper);
//        if(employee ==null){
//            return null;
//        }
//        return EmployeeAssembler.INSTANCE.entityToVO(employee);

        // 先从缓存取
        String json = stringRedisTemplate.opsForValue().get("employee_" + id);
        if (StringUtils.hasText(json)) {
            return GSON.fromJson(json, EmployeeVO.class);
        }
        Employee employee = employeeMapper.selectById(id);
        EmployeeVO vo = EmployeeAssembler.INSTANCE.entityToVO(employee);
        stringRedisTemplate.opsForValue().set("employee_" + id, GSON.toJson(vo));
        return vo;
    }
}